#!/bin/bash
# liho-browser-mcp: the Playwright MCP server for the agent (stdio), wired to the
# sandbox egress proxy. Chromium cannot take proxy credentials on its command
# line, so the credentials from HTTPS_PROXY go into a private Playwright config
# (launchOptions.proxy with username/password); the proxy answers 407 and
# Playwright authenticates. Screenshots and traces land in /work/browser.
set -euo pipefail

out_dir="/work/browser"
mkdir -p -- "${out_dir}"
cfg="$(mktemp /tmp/liho-browser-XXXXXX.json)"
chmod 600 -- "${cfg}"

node - "${cfg}" "${out_dir}" <<'JS'
const fs = require('node:fs');
const [out, outputDir] = process.argv.slice(2);
const raw = process.env.HTTPS_PROXY || process.env.https_proxy || '';
const launchOptions = { headless: true, chromiumSandbox: false };
if (raw) {
  const u = new URL(raw);
  launchOptions.proxy = {
    server: `${u.protocol}//${u.host}`,
    username: decodeURIComponent(u.username),
    password: decodeURIComponent(u.password),
    bypass: '127.0.0.1,localhost',
  };
}
const config = { browser: { browserName: 'chromium', isolated: true, launchOptions }, outputDir };
fs.writeFileSync(out, JSON.stringify(config), { mode: 0o600 });
JS

exec playwright-mcp --config "${cfg}" --headless --isolated --output-dir "${out_dir}"
